#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QApplication>
#include <cassert>

#define ASSERT(val) assert(val)

#include <QCloseEvent>

#include <QGridLayout>
#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QSystemTrayIcon>

#include <QMessageBox>
#include <QAction>
#include <QMenu>

#include <QTimer>
#include <QRegExp>
#include <QRegExpValidator>

#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QHostAddress>



class MyDialog: public QDialog{
    Q_OBJECT
public:
    MyDialog(QWidget* = 0);
private:
    enum AlarmEventType{
        AlarmEventNoResponse,
        AlarmEventAlarm
    };

    void alarmEvent(AlarmEventType){
        return;
    }


private slots:


    void update(){
        ASSERT(socket != 0);
        if(socket->state() == QTcpSocket::UnconnectedState){
            //hostTryConnect();
            return;
        }
        socket->write("GET /?buttonState");
        if(!socket->waitForBytesWritten(1000)){
            connectionLabel->setText("Error then writeing");
            socket->close();
            return;
        }
        if (!socket->waitForReadyRead(1000)) {
            socket->close();
            connectionLabel->setText("No response");
            return;
        }
        int by = socket->bytesAvailable();
        char* data = new char[by];
        if(socket->read(data,by) != -1){
            QString message(data);
            if(message.contains("NotPushed")){
                statusLabel->setText("Not Pushed");
            }
            else{
                statusLabel->setText("Pushed");
            }
        }
        else {
            connectionLabel->setText("Could not read");
            alarmEvent(AlarmEventNoResponse);
        }
    }


private:
    QGridLayout* mainLayout;
    QPushButton* ipChangePushButton;
    QLineEdit* ipLineEdit;
    QPushButton* maskChangePushButton;
    QLineEdit* maskLineEdit;
    QLabel* statusLabel;
    QLabel* connectionLabel;
    QLineEdit* connectLineEdit;
    QPushButton* connectButton;
    QTcpSocket* socket;
    QTimer* timer;
    QSystemTrayIcon* tray;
    bool connected;
};


#endif // MYDIALOG_H
